package com.opc.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opc.dto.AbnormalDto;
import com.opc.dto.DeviceRecordDto;
import com.opc.dto.SelectData;
import com.opc.entity.Device;
import com.opc.entity.PageRes;
import com.opc.mapper.DeviceMapper;
import com.opc.mapper.DeviceRecordLatestMapper;
import com.opc.mapper.DeviceRecordMapper;
import com.opc.repository.DeviceRepository;
import com.opc.service.DeviceService;
import com.opc.utils.StringUtils;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2023-03-13
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private DeviceRecordLatestMapper recordLatestMapper;
    @Resource
    private DeviceRepository repository;
    @Resource
    private DeviceRecordMapper recordMapper;

    @Override
    public List<String> selectAllDevices() {
        return deviceMapper.selectAllDevices();
    }

    @Override
    public List<SelectData> getProductLine() {
        List<SelectData> list = new ArrayList<>();
        List<String> productLine = deviceMapper.getProductLine();
        for (String line : productLine) {
            SelectData selectData = new SelectData();
            selectData.setName(line);
            selectData.setValue(line);
            list.add(selectData);
        }
        return list;
    }

    @Override
    public PageRes<DeviceRecordDto> getRecordListByLine(String proLine, Integer pageNo, Integer pageSize) {
        Map<String, Device> deviceMap = repository.getAllDevicesByLine(proLine);
        Page<DeviceRecordDto> page = new Page<>(pageNo, pageSize);
        IPage<DeviceRecordDto> records = recordLatestMapper.getRecordListByLine(page, proLine);
        if (records.getTotal() == 0) {
            return new PageRes<>(1, 0, 0, null);
        }
        List<DeviceRecordDto> list = records.getRecords();
        for (DeviceRecordDto deviceRecordDto : list) {
            if (deviceMap.containsKey(deviceRecordDto.getDeviceCode())) {
                Device d = deviceMap.get(deviceRecordDto.getDeviceCode());
                deviceRecordDto.setDeviceName(d.getDeviceName());
                deviceRecordDto.setProductionLine(d.getProductionLine());
                deviceRecordDto.setStationName(d.getStationName());
                deviceRecordDto.setStationNum(d.getStationNum());
            }
        }
        return new PageRes<>(pageNo, pageSize, records.getTotal(), list);
    }

    @Override
    public List<List<Object>> getRecordListByLineNoPages(String proLine) {
        List<List<Object>> returnList = new ArrayList<>();
        List<Device> list = deviceMapper.getAllDevicesByLine(proLine);
        List<DeviceRecordDto> records = recordLatestMapper.getRecordListByLineNoPages(proLine);
        Map<String, DeviceRecordDto> map = new HashMap<>();
        for (DeviceRecordDto record : records) {
            map.put(record.getDeviceCode(), record);
        }
        for (Device d : list) {
            List<Object> objList = new ArrayList<>();
            objList.add(d.getStationNum() + " " + d.getStationName() + " " + d.getDeviceName());
            if (map.containsKey(d.getDeviceCode())) {
                DeviceRecordDto device = map.get(d.getDeviceCode());
                objList.add(device.getCountVal());
                objList.add(device.getSetVal());
            } else {
                objList.add(0);
                objList.add(0);
            }
            returnList.add(objList);
        }
        return returnList;
    }

    @Override
    public Map<String, List<Object>> getAbnormalStatistic() {
        Date nowTime = new Date();
        DateTime startTime = DateUtil.beginOfMonth(nowTime);
        DateTime endTime = DateUtil.endOfMonth(nowTime);
        List<AbnormalDto> list = recordMapper.getAbnormalStatistic(startTime, endTime);
        Map<String, List<Object>> map = new HashMap<>();
        List<Object> abList = new ArrayList<>();
        List<Object> lineList = new ArrayList<>();
        for (AbnormalDto abnormalDto : list) {
            abList.add(abnormalDto.getAbnormalNum());
            lineList.add(abnormalDto.getProductionLine());
        }
        map.put("list", abList);
        map.put("lineList", lineList);
        return map;
    }

    @Override
    public List<String> selectAllProLine() {
        return deviceMapper.selectAllProLine();
    }

    @Override
    public List<SelectData> getDeviceListByConditions(String proLine, String stationNum) {
        Map<String, Object> paramMap = new ConcurrentHashMap<>(2);
        paramMap.put("proLine", proLine);
        paramMap.put("stationNum", stationNum);
        List<SelectData> selectDataList = new ArrayList<>();
        List<Device> list = deviceMapper.getDeviceListByConditions(paramMap);
        if (StringUtils.isBlank(stationNum)) {
            for (Device device : list) {
                SelectData selectData = new SelectData();
                selectData.setName(device.getStationNum() + " " + device.getStationName());
                selectData.setValue(device.getStationNum());
                if (!selectDataList.contains(selectData)) {
                    selectDataList.add(selectData);
                }
            }
        } else {
            for (Device device : list) {
                SelectData selectData = new SelectData();
                selectData.setName(device.getDeviceName());
                selectData.setValue(device.getDeviceCode());
                selectDataList.add(selectData);
            }
        }
        return selectDataList;
    }
}
